package com.kingzoon.seckill.service.impl.account;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import payment.tools.util.GUID;
import payment.tools.util.TimeUtil;

import com.kingzoon.seckill.dao.UaCapAcctInfoMapper;
import com.kingzoon.seckill.dao.UaCapChannelMapper;
import com.kingzoon.seckill.dao.UaMemberInfoMapper;
import com.kingzoon.seckill.dto.Result;
import com.kingzoon.seckill.entity.UaCapAcctInfo;
import com.kingzoon.seckill.entity.UaCapAcctInfoKey;
import com.kingzoon.seckill.entity.UaCapChannel;
import com.kingzoon.seckill.entity.UaCapChannelExample;
import com.kingzoon.seckill.entity.UaMemberInfo;
import com.kingzoon.seckill.entity.UaMemberInfoExample;
import com.kingzoon.seckill.enums.MacroDefine;
import com.kingzoon.seckill.service.OpenAcctService;
import com.kingzoon.seckill.util.UtilTools;

@Service
public class OpenAcctServiceImpl implements OpenAcctService {

	private HashMap<String, Object> paramMap = new HashMap<String, Object>();
	
	@Autowired
	private UaCapChannelMapper uaCapChannelDao;
	
	@Autowired
	private UaCapAcctInfoMapper uaCapAcctInfoDao;
	
	@Autowired
	private UaMemberInfoMapper uaMemberInfoDao;
	
	private UaCapChannelExample uaCapChannelExample = new UaCapChannelExample();
	private UaCapChannel uaCapChannel = new UaCapChannel();
	private UaCapAcctInfoKey uaCapAcctInfoKey = new UaCapAcctInfoKey();
	private UaCapAcctInfo uaCapAcctInfo = new UaCapAcctInfo();
	private UaMemberInfo uaMemberInfo = new UaMemberInfo();
	private UaMemberInfoExample uaMemberInfoExample = new UaMemberInfoExample();

	public Result<HashMap<String, Object>> doBusiness(HashMap<String, Object> mapParam, Result<HashMap<String, Object>> rs) {
		paramMap = mapParam;
		if (preCheck(rs)) {
			if (cache(rs)) {
				if (check(rs)) {
					if (before(rs)) {
						if (clear(rs)) {
							after(rs);
						}
					}
				}
			}
		}
		return rs;
	}

	public boolean preCheck(Result<HashMap<String, Object>> rs) {
		String[] keys = {"exchangeId","distributorId","memberId","channelType","smsCode","idType","idCode","clientName"};
		String emptyString = UtilTools.isParamEmpty(keys, paramMap);
		if(!UtilTools.isEmpty(emptyString)){
			rs.setFlag(false);
			rs.setCode(MacroDefine.GS_CODE.E_1002.getCode());
			rs.setMessage(MacroDefine.GS_CODE.E_1002.getMessage());
			return false;
		}
		return true;
	}

	public boolean cache(Result<HashMap<String, Object>> rs) {
		return true;
	}

	public boolean check(Result<HashMap<String, Object>> rs) {
		//校验渠道表
		uaCapChannelExample.createCriteria().andChannelTypeEqualTo(UtilTools.valueOf(paramMap.get("channelType")))
		.andMemberIdEqualTo(UtilTools.valueOf(paramMap.get("memberId")));
		List<UaCapChannel> list = uaCapChannelDao.selectByExample(uaCapChannelExample);
		if(list != null && list.size() > 0){
			uaCapChannel = list.get(0);
			//校验渠道签约状态
			uaCapAcctInfoKey.setExchangeId(UtilTools.valueOf(paramMap.get("exchangeId")));
			uaCapAcctInfoKey.setDistributorId(UtilTools.valueOf(paramMap.get("distributorId")));
			uaCapAcctInfoKey.setMemberId(uaCapChannel.getMemberId());
			uaCapAcctInfoKey.setCapitalAccount(uaCapChannel.getCapitalAccount());
			uaCapAcctInfo = uaCapAcctInfoDao.selectByPrimaryKey(uaCapAcctInfoKey);
			
			if("0".equals(uaCapAcctInfo.getAccountStatus())){//0-正常
				rs.setFlag(false);
				rs.setCode(MacroDefine.ZH_CODE.E_5416.getCode());
				rs.setMessage(MacroDefine.ZH_CODE.E_5416.getMessage());
				return false;
			}else if("1".equals(uaCapAcctInfo.getAccountStatus())){//1-冻结
				rs.setFlag(false);
				rs.setCode(MacroDefine.ZH_CODE.E_5402.getCode());
				rs.setMessage(MacroDefine.ZH_CODE.E_5402.getMessage());
				return false;
			}
		}
		return true;
	}

	public boolean before(Result<HashMap<String, Object>> rs) {
		String capitalAccount = GUID.getTxNo16();
		String accountStatus = "0";//0-正常
		paramMap.put("capitalAccount", capitalAccount);
		paramMap.put("accountStatus", accountStatus);
		paramMap.put("insertDate", TimeUtil.getTimeStamp().substring(0, 8));
		paramMap.put("insertTime", TimeUtil.getTimeStamp().substring(8));
		paramMap.put("curBalance", new BigDecimal(0));
		paramMap.put("availableBalance", new BigDecimal(0));
		paramMap.put("curTradeFrozen", new BigDecimal(0));
		paramMap.put("curWithdrawFrozen", new BigDecimal(0));
		paramMap.put("curAbnormalFrozen", new BigDecimal(0));
		paramMap.put("curDepositFrozen", new BigDecimal(0));
		paramMap.put("curAvailableWithdraw", new BigDecimal(0));
		paramMap.put("fundInTransit", new BigDecimal(0));
		paramMap.put("currencyType", UtilTools.isEmpty(UtilTools.valueOf(paramMap.get("currencyType"))) ? "156" : paramMap.get("currencyType"));
		paramMap.put("lastBalance", new BigDecimal(0));
		paramMap.put("updateDate", paramMap.get("insertDate"));
		paramMap.put("updateTime", paramMap.get("insertTime"));
		paramMap.put("accountType", UtilTools.isEmpty(UtilTools.valueOf(paramMap.get("accountType"))) ? "0" : paramMap.get("accountType"));
		uaCapAcctInfo.setExchangeId(UtilTools.valueOf(paramMap.get("exchangeId")));
		uaCapAcctInfo.setDistributorId(UtilTools.valueOf(paramMap.get("distributorId")));
		uaCapAcctInfo.setMemberId(UtilTools.valueOf(paramMap.get("memberId")));
		uaCapAcctInfo.setCapitalAccount(UtilTools.valueOf(paramMap.get("capitalAccount")));
		uaCapAcctInfo.setChannelType(UtilTools.valueOf(paramMap.get("channelType")));
		uaCapAcctInfo.setAccountStatus(UtilTools.valueOf(paramMap.get("accountStatus")));
		uaCapAcctInfo.setInsertDate(Long.valueOf((String)paramMap.get("insertDate")));
		uaCapAcctInfo.setInsertTime(Long.valueOf((String)paramMap.get("insertTime")));
		uaCapAcctInfo.setCurBalance((BigDecimal)paramMap.get("curBalance"));
		uaCapAcctInfo.setAvailableBalance((BigDecimal)paramMap.get("availableBalance"));
		uaCapAcctInfo.setCurTradeFrozen((BigDecimal)paramMap.get("curTradeFrozen"));
		uaCapAcctInfo.setCurWithdrawFrozen((BigDecimal)paramMap.get("curWithdrawFrozen"));
		uaCapAcctInfo.setCurAbnormalFrozen((BigDecimal)paramMap.get("curAbnormalFrozen"));
		uaCapAcctInfo.setCurDepositFrozen((BigDecimal)paramMap.get("curDepositFrozen"));
		uaCapAcctInfo.setCurAvailableWithdraw((BigDecimal)paramMap.get("curAvailableWithdraw"));
		uaCapAcctInfo.setFundInTransit((BigDecimal)paramMap.get("fundInTransit"));
		uaCapAcctInfo.setCurrencyType(UtilTools.valueOf(paramMap.get("currencyType")));
		uaCapAcctInfo.setLastBalance((BigDecimal)paramMap.get("lastBalance"));
		uaCapAcctInfo.setUpdateDate(Long.valueOf((String)paramMap.get("updateDate")));
		uaCapAcctInfo.setUpdateTime(Long.valueOf((String)paramMap.get("updateTime")));
		uaCapAcctInfo.setAccountType(UtilTools.valueOf(paramMap.get("accountType")));
		return true;
	}

	@Transactional
	public boolean clear(Result<HashMap<String, Object>> rs) {
		//TODO 实名验证
		HashMap<String,Object> reqMap = new HashMap<String, Object>();
		reqMap.put("idType", paramMap.get("idType"));
		reqMap.put("idCode", paramMap.get("idCode"));
		reqMap.put("clientName", paramMap.get("clientName"));
		boolean flag = acctVerify(reqMap);
		if(!flag){
			rs.setFlag(false);
			rs.setCode(MacroDefine.ZH_CODE.E_5417.getCode());
			rs.setMessage(MacroDefine.ZH_CODE.E_5417.getMessage());
			return false;
		}
		
		//写入资金账户信息表
		int a = uaCapAcctInfoDao.insertSelective(uaCapAcctInfo);
		if(a != 1){
			rs.setFlag(false);
			rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
			rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
			return false;
		}
		
		//写入资金账户签约渠道关系表
		uaCapChannel.setExchangeId(uaCapAcctInfo.getExchangeId());
		uaCapChannel.setDistributorId(uaCapAcctInfo.getDistributorId());
		uaCapChannel.setMemberId(uaCapAcctInfo.getMemberId());
		uaCapChannel.setCapitalAccount(uaCapAcctInfo.getCapitalAccount());
		uaCapChannel.setChannelType(uaCapAcctInfo.getChannelType());
		int c = uaCapChannelDao.insertSelective(uaCapChannel);
		if(c != 1){
			rs.setFlag(false);
			rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
			rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
			return false;
		}
		
		//更新会员信息表
		uaMemberInfo.setMemberName(UtilTools.valueOf(paramMap.get("clientName")));
		uaMemberInfoExample.createCriteria().andMemberIdEqualTo(uaCapAcctInfo.getMemberId());
		int b = uaMemberInfoDao.updateByExampleSelective(uaMemberInfo, uaMemberInfoExample);
		if(b != 1){
			rs.setFlag(false);
			rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
			rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
			return false;
		}
		return true;
	}

	public boolean after(Result<HashMap<String, Object>> rs) {
		HashMap<String, Object> respMap = new HashMap<String,Object>();
		respMap.put("exchangeId", uaCapAcctInfo.getExchangeId());
		respMap.put("distributorId", uaCapAcctInfo.getDistributorId());
		respMap.put("memberId", uaCapAcctInfo.getMemberId());
		respMap.put("capitalAccount", uaCapAcctInfo.getCapitalAccount());
		respMap.put("accountStatus", uaCapAcctInfo.getAccountStatus());
		respMap.put("curBalance", uaCapAcctInfo.getCurBalance());
		respMap.put("availableBalance", uaCapAcctInfo.getAvailableBalance());
		respMap.put("curTradeFrozen", uaCapAcctInfo.getCurTradeFrozen());
		respMap.put("curWithdrawFrozen", uaCapAcctInfo.getCurWithdrawFrozen());
		respMap.put("curAbnormalFrozen", uaCapAcctInfo.getCurAbnormalFrozen());
		respMap.put("curAvailableWithdraw", uaCapAcctInfo.getCurAvailableWithdraw());
		respMap.put("accountType", uaCapAcctInfo.getAccountType());
		rs.setData(respMap);
		rs.setFlag(true);
		rs.setCode(MacroDefine.JC_CODE.S_0000.getCode());
		rs.setMessage(MacroDefine.JC_CODE.S_0000.getMessage());
		return true;
	}
	
	public boolean acctVerify(HashMap<String,Object> reqMap){
		//TODO 实名验证通过
		return true;
	}

}
